package org.cc.cloud;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 * 字符串的MessageConverter
 */
public class TextMessageConverter implements MessageConverter {

	@Override
	public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
		System.out.println("==========toMessage========");
		return new Message(object.toString().getBytes(), messageProperties);
	}

	@Override
	public Object fromMessage(Message message) throws MessageConversionException {
		System.out.println("==========fromMessage========");
		String contentType = message.getMessageProperties().getContentType();
		
		if(contentType != null) {
			if(contentType.contains("text")) {
				return new String(message.getBody()); 
			} 
			else if (contentType.contains("body")) {
				return new Body(message.getBody());
			}
		}
		return message.getBody();
	}

}
